package com.server.shop;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.config.AppReturn;
import com.server.user.AppUserBaseCtrl;
import com.util.AliOssUtil;
import com.util.StrKit;

/**
 * 需要登录得商店操作
 * @author dell
 *
 */
@Controller
@RequestMapping("/appUser/shop")
public class ShopCtrl extends AppUserBaseCtrl{

	@Autowired
	ShopService shopService;
	
	/**
	 * 上传图片
	 * @param file
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/upPic")
	public String upPic(@RequestParam("file")MultipartFile multiFile)
	{
		 // 获取文件名
        String fileName = multiFile.getOriginalFilename();
        System.out.println("finleName : "+fileName);
        // 获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        JSONObject jso = new JSONObject();
		try {
			File file = File.createTempFile(fileName, prefix);
	        multiFile.transferTo(file);
			jso = AliOssUtil.upPic(file, getReq());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return jso.toString();
	}

	/**
	 * 付款加盟
	 * @param phone
	 * @param name
	 * @param day
	 * @param yytime
	 * @param goodsid
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/doShopJoin")
	public String doShopJoin(@RequestParam("phone")String phone,@RequestParam("name")String name,@RequestParam(name="addr",required=false)String addr,@RequestParam("goodsid")String goodsid,@RequestParam("dkpzurl")String dkpzurl)
	{
		AppReturn ret = shopService.doShopJoin(getLogUser(), phone, name,  addr, goodsid,dkpzurl);
		return ret.toString();
	}
	
	/**
	 * 查询自己的购物车
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getMyCar")
	public String getMyCar()
	{
		AppReturn ret = shopService.getMyCar(getLogUser());
		return ret.toString();
	}
	
	/**
	 * 增加到购物车
	 * @param id
	 * @param stockid 库存ID
	 * @param num
	 */
	@ResponseBody
	@RequestMapping("/addCar")
	public String addCar(@RequestParam("id")String id,@RequestParam("stockid")String stockid,@RequestParam(name="num",required=false,defaultValue="1")String num)
	{
		
		AppReturn ret = shopService.addCar(getLogUser(), id, stockid, num);
		
		return ret.toString();
	}
	
	/**
	 * 修改购物车商品数量
	 * @param id 购物车ID
	 * @param num
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/upCar")
	public String upCar(@RequestParam("id")String id,@RequestParam(name="num",required=false,defaultValue="1")String num)
	{
		AppReturn ret = shopService.upCar(getLogUser(), id, num);
		return ret.toString();
	}
	/**
	 * 删除购物车中商品
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delCar")
	public String delCar(@RequestParam("id")String id)
	{
		AppReturn ret = shopService.delCar(getLogUser(), id);
		return ret.toString();
	}
	/**
	 * 直接购买商品
	 * @param id
	 * @param num
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/buyGoods")
	public String buyGoods(@RequestParam("goodsId")String goodsId,@RequestParam("stockid")String stockid,@RequestParam(name="num",required=false,defaultValue="1")String num,@RequestParam("addrid")String addrid)
	{
		AppReturn ret = shopService.buyGoods(getLogUser(), goodsId, stockid, num, addrid);
		return ret.toString();
	}
	
	/**
	 * 结算购物车中得商品
	 * @param ids
	 * @param num
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/buyCarGoods")
	public String buyCarGoods(@RequestParam("ids")String ids,@RequestParam("addrid")String addrid)
	{
		AppReturn ret = shopService.buyCarGoods(getLogUser(), ids, addrid);
		return ret.toString();
	}
	
//	/**
//	 * 根据商品信息生成订单
//	 * @param json
//	 * @return
//	 */
//	@ResponseBody
//	@RequestMapping(value ="/createList" )
//	public String createList()
//	{
//		JSONObject goods = new JSONObject(getBodyData());
//		
//		AppReturn ret = shopService.createList(getLogUser(), goods);
//		return ret.toString();
//	}
//	
//
//	

	
	/**
	 * 根据商品和收货地址，返回邮费信息——现在都是免费
	 * @param goodsid
	 * @param addrid
	 * @param num
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getPostFee" ,produces = "application/json;charset=UTF-8")
	public String getPostFee(HttpServletRequest request)
	{
		String body = getBodyData();
		JSONObject goods = new JSONObject(body);
//		String addrid=goods.getString("addrid");
//		System.out.println("下单地址ID："+addrid);
//		JSONArray jsa = goods.getJSONArray("goods");
//		System.out.println("共有数据："+jsa.length()+"条");
//		
//		
//		JSONObject data = new JSONObject();
//		data.put("fee", "0");
//		data.put("收到下单地址ID", addrid);
		AppReturn ret = shopService.getPostFee(getLogUser(), goods);
		return ret.toString();
	}
}
